import request from '@/utils/request'
import RefundsForm from './../refunds/Form/index.vue'
import ExportBox from './ExportBox/index.vue'
import CustomForm from './Form/index.vue'
import FormPrint from './FormPrint/index.vue'
export default {
  components: { CustomForm, ExportBox, RefundsForm, FormPrint },
  data() {
    return {
      showAll: false,
      query: {
        enCode: undefined,
        name: undefined,
        method: undefined,
        status: undefined,
        campus: undefined,
        time: undefined
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'time'
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [{ prop: 'enCode', label: '订单编号' }, { prop: 'name', label: '客户名称' }, { prop: 'campusName', label: '校区名称' }, { prop: 'brand', label: '收款品牌' }, { prop: 'method', label: '收款方式' }, { prop: 'amount', label: '收款金额' }, { prop: 'time', label: '收款时间' }, { prop: 'type', label: '收款类型' }, { prop: 'status', label: '订单状态' }, { prop: 'finish', label: '是否完款' }, { prop: 'managerId', label: '招商经理' }],
      brandOptions: [],
      methodOptions: [{ fullName: '刷卡', id: '1' }, { fullName: '转账', id: '2' }, { fullName: '扫码', id: '3' }, { fullName: '现金', id: '4' }],
      typeOptions: [{ fullName: '加盟', id: '1' }, { fullName: '续约', id: '2' }, { fullName: '代理', id: '3' }],
      managerIdOptions: [],
      statusOptions: [{ fullName: '全款', id: '1' }, { fullName: '定金', id: '2' }, { fullName: '保证金', id: '3' }, { fullName: '余款', id: '4' }],
      refundsFormVisible: false,
      printFormVisible: false,
      campusOptions: [],
      campusLoading: false
    }
  },
  computed: {},
  created() {
    this.initData()
  },
  mounted() {
    this.getUserList()
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  methods: {
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.managerIdOptions = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/finance/receipt`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    /*
    *
    *   this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail)
      })
    * */
    handlePrint(id, row) {
      this.printFormVisible = true
      this.$nextTick(() => {
        this.$refs.PrintForm.init(id, row)
      })
    },
    handleRefunds(id, row) {
      this.refundsFormVisible = true
      this.$nextTick(() => {
        this.$refs.refundsForm.receiptInit(id, row)
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/finance/receipt/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    handleSelectionChange(val) {
      const res = val.map(item => item.id)
      this.multipleSelection = res
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: 'error',
          message: '请选择一条数据',
          duration: 1500
        })
        return
      }
      const ids = this.multipleSelection
      this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/system/FinanceReceipt/batchRemove`,
            method: 'POST',
            data: ids
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(id, isDetail, isEdit) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail, isEdit)
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query }
      request({
        url: `/api/finance/receipt/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        if (!res.data.url) return
        window.location.href = this.define.comUrl + res.data.url
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: 'time'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: 'time'
      }
      this.initData()
    }
  }
}
